package com.gxuwz.eschool;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.gxuwz.eschool.fragment.CardFragment;
import com.gxuwz.eschool.fragment.HomeFragment;
import com.gxuwz.eschool.fragment.MessageFragment;
import com.gxuwz.eschool.fragment.MyFragment;

public class MainActivity extends AppCompatActivity {

    private TextView tv_home;
    private TextView tv_card;
    private TextView tv_message;
    private TextView tv_my;
    private HomeFragment homeFragment;
    private CardFragment cardFragment;
    private MessageFragment messageFragment;
    private MyFragment myFragment;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_home= findViewById(R.id.tv_home);
        tv_card= findViewById(R.id.tv_card);
        tv_message=findViewById(R.id.tv_message);
        tv_my =findViewById(R.id.tv_my);
        tv_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragment(homeFragment);
            }
        });
        tv_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragment(cardFragment);
            }
        });
       tv_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragment(messageFragment);
            }
        });
        tv_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragment(myFragment);
            }
        });


        //创建Fragment
        homeFragment=new HomeFragment();
        cardFragment=new CardFragment();
        messageFragment=new MessageFragment();
        myFragment=new MyFragment();
        setFragment(homeFragment);
    }
    private void setFragment(Fragment fragment){
        fragmentTransaction =getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_container,fragment);
        fragmentTransaction.commit();

    }

}